package com.jackstraw.jack_base.remote_command.controller;


import com.jackstraw.jack_base.remote_command.entity.InitVo;
import com.jackstraw.jack_base.remote_command.entity.InstallProperties;
import com.jackstraw.jack_base.remote_command.entity.NonPasswordProperties;
import com.jackstraw.jack_base.remote_command.pool.entity.SessiontConnectionConfig;
import com.jackstraw.jack_base.remote_command.service.InstallService;
import com.jackstraw.jack_base.remote_command.service.GroupControlService;
import com.jackstraw.jack_base.remote_command.service.impl.AbstractGroupControlServiceImpl;
import com.jackstraw.jack_base.util.JsonUtil;
import com.jackstraw.jack_base.util.SpringUtil;
import com.jackstraw.jack_base.util.exception.BadRequestException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/install")
public class InstallController {

    @Autowired
    private InstallService installService;

    @Autowired
    private GroupControlService groupControlService;

    @PutMapping("/initNotOther")
    public void initNotOther(@RequestBody InitVo initVo) {
        try {
            installService.init(SessiontConnectionConfig.getRootConnName(initVo.getConnName()));
        } catch (Exception e) {
            log.error("initNotOther异常，异常原因为：{}", e);
            throw new BadRequestException("3400000001", "initNotOther异常");
        }
    }

    @PutMapping("/init")
    public void init(@RequestBody InitVo initVo) {
        try {
            installService.init(initVo);
        } catch (Exception e) {
            log.error("init异常，异常原因为：{}", e);
            throw new BadRequestException("3300000002", "init异常");
        }
    }

    @PutMapping("/envInit")
    public void envInit(@RequestBody InitVo initVo) {
        try {
            installService.envInit(initVo);
        } catch (Exception e) {
            log.error("envInit异常，异常原因为：{}", e);
            throw new BadRequestException("3300000004", "envInit异常");
        }
    }

    @PutMapping("/nonPassword")
    public void nonPassword(@RequestBody NonPasswordProperties nonPasswordProperties) {
        try {
            groupControlService.nonPassword(nonPasswordProperties);
        } catch (Exception e) {
            log.error("nonPassword异常，免密登录集群地址为:{},异常原因为:{}", nonPasswordProperties.getConnNameList(), e);
            throw new BadRequestException("3300000006", "nonPassword异常");
        }
    }


    @PutMapping("/install")
    public String install(@RequestBody InstallProperties installProperties) {
        String installName = installProperties.getInstallName();
        GroupControlService groupControlService = SpringUtil.getBean(installName.concat(AbstractGroupControlServiceImpl.BEAN_NAME), GroupControlService.class);
        if (ObjectUtils.isEmpty(groupControlService)) {
            groupControlService = SpringUtil.getBean(GroupControlService.class);
        }
        try {
            groupControlService.install(installProperties);
            if (installProperties.getNoteMap().size() == 0) {
                return "seccuss";
            } else {
                return JsonUtil.obj2String(installProperties.getNoteMap());
            }
        } catch (Exception e) {
            log.error("install异常，异常原因为：{}", e);
            throw new BadRequestException("3300000005", "install异常");
        }
    }

}
